package com.cskaoyan.shopping.service.impl;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.shopping.constants.GlobalConstants;
import com.cskaoyan.shopping.converter.PanelConverter;
import com.cskaoyan.shopping.converter.ProductCateConverter;
import com.cskaoyan.shopping.dal.entitys.ItemCat;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContent;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemCatMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.IHomeService;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author lizhenhao
 * @ClassName IHomeServiceImpl.java
 * @Description TODO
 * @createTime 2022年06月17日 16:18:00
 */
@Service
public class IHomeServiceImpl implements IHomeService {

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    PanelConverter panelConverter;

    @Autowired
    ItemCatMapper itemCatMapper;

    @Autowired
    ProductCateConverter productCateConverter;

    @Autowired
    RedissonClient redissonClient;

    @Override
    public HomePageResponse homepage() {

        RBucket<Object> bucket = redissonClient.getBucket(GlobalConstants.HOMEPAGE_CACHE_KEY);

        if (bucket.get() != null){

            return (HomePageResponse) bucket.get();
        }

        List<Panel> panels = panelMapper.selectPanel();

        List<PanelDto> panelDtos = panelConverter.panels2Dto(panels);

        Set<PanelDto> panelDtoSet = new HashSet<>(panelDtos.size());

        for (int i = 0; i < panelDtos.size(); i++) {

            PanelDto panelDto = new PanelDto();

            BeanUtils.copyProperties(panelDtos.get(i),panelDto);

            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panelDtos.get(i).getId());

            List<PanelContentItemDto> panelContentItemDtos = panelConverter.panelContentItems2Dto(panelContentItems);

            panelDto.setPanelContentItems(panelContentItemDtos);

            panelDtoSet.add(panelDto);

        }

        HomePageResponse homePageResponse = new HomePageResponse();
        homePageResponse.setPanelContentItemDtos(panelDtoSet);

        RBucket<Object> rBucket = redissonClient.getBucket(GlobalConstants.HOMEPAGE_CACHE_KEY);
        rBucket.set(homePageResponse);
        rBucket.expire(GlobalConstants.HOMEPAGE_EXPIRE_TIME, TimeUnit.DAYS);

        return homePageResponse;
    }

    @Override
    public List<PanelContentDto> navigation() {

        RBucket<Object> bucket = redissonClient.getBucket(GlobalConstants.HEADER_PANEL_CACHE_KEY);

        if (bucket.get() != null){

            return (List<PanelContentDto>) bucket.get();
        }

        List<PanelContent> list = panelContentMapper.selectByPanelId(GlobalConstants.HEADER_PANEL_ID);

        List<PanelContentDto> panelContentDtos = panelConverter.panelContents2Dto(list);

        RBucket<Object> rBucket = redissonClient.getBucket(GlobalConstants.HEADER_PANEL_CACHE_KEY);
        rBucket.set(panelContentDtos);
        rBucket.expire(GlobalConstants.HEADER_PANEL_EXPIRE_TIME, TimeUnit.DAYS);

        return panelContentDtos;
    }

    @Override
    public List<ProductCateDto> categories() {

        RBucket<Object> bucket = redissonClient.getBucket(GlobalConstants.PRODUCT_CATE_CACHE_KEY);

        if (bucket.get() != null){

            return (List<ProductCateDto>) bucket.get();
        }

        Example example = new Example(ItemCat.class);
        example.createCriteria().andEqualTo("parentId",1);

        List<ItemCat> itemCats = itemCatMapper.selectAll();

        List<ProductCateDto> productCateDtos = productCateConverter.items2Dto(itemCats);

        RBucket<Object> rBucket = redissonClient.getBucket(GlobalConstants.PRODUCT_CATE_CACHE_KEY);
        rBucket.set(productCateDtos);
        rBucket.expire(GlobalConstants.PRODUCT_CATE_EXPIRE_TIME, TimeUnit.DAYS);

        return productCateDtos;
    }
}
